package com.cqcvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cqcvc.entity.*;
import com.cqcvc.mapper.*;
import com.cqcvc.service.UserRoleService;
import com.cqcvc.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqcvc.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.openmbean.ArrayType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hunter
 * @since 2022-08-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("username",username);
        User user = userMapper.selectOne(qw);
        return user;
    }

    @Override
    public List<Map<String, Object>> getMenuList(String username) {
        List<Map<String, Object>> list=new ArrayList<>();
        //获取父权限
        List<Permission> fatherPermissionsList=permissionMapper.selectFatherPermissionsByUsername(username);
        for(Permission fatherP:fatherPermissionsList){
            Map<String,Object> map=new HashMap<>();
            map.put("menu",fatherP);
            //获取子权限
            List<Permission> subPermissionsList=permissionMapper.selectSubPermissionsByFatherId(username,fatherP.getPermissionId());
            map.put("subMenu",subPermissionsList);
            list.add(map);
        }

        return list;
    }

    @Override
    public Map<String,Object> selectUser(String pageNum, String pageSize,String username,String sex) {
        int first = StringUtil.isNullOrEmpty(pageNum) ? 1 : Integer.parseInt(pageNum);
        int second = StringUtil.isNullOrEmpty(pageSize) ? 6 : Integer.parseInt(pageSize);
        PageHelper.startPage(first,second);
        QueryWrapper qw=new QueryWrapper();
        if(!StringUtil.isNullOrEmpty(username)){
            qw.like("username",username);
        }
        if(!StringUtil.isNullOrEmpty(sex)){
            qw.like("sex",sex);
        }

        List<User> list=userMapper.selectList(qw);
        PageInfo pageInfo=new PageInfo(list);
        Map<String, Object> map=new HashMap<>();
        map.put("total",pageInfo.getTotal());
        List<Map<String,Object>> mapList=new ArrayList<>();
        for(User user:list){
            Map<String,Object> map1=new HashMap<>();
            map1.put("user",user);
            mapList.add(map1);
        }
        map.put("list",mapList);
        return  map;
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public int saveUser(User user,String[] arr) {
        int i;
        user.setSalt(String.valueOf(System.currentTimeMillis()));
        user.setPassword(StringUtil.md5(user.getPassword()+user.getSalt()));
        //如果id为空则新增用户否则就保存用户
        if(StringUtil.isNullOrEmpty(user.getUserId())){
            i=userMapper.insert(user);
        }else{
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("user_id",user.getUserId());
            i=userMapper.update(user,updateWrapper);
        }

        //根据角色的中文名称，获取新角色名对应的id
        List<Integer> roleIds=new ArrayList<>();
        for(String roleName:arr){
            QueryWrapper qw=new QueryWrapper<>();
            qw.eq("role_name",roleName);
            Role role=roleMapper.selectOne(qw);
            roleIds.add(role.getRoleId());
        }
        //获取被编辑用户的id
        System.out.println("被编辑用户ID:"+user.getUserId());
        Integer uid=user.getUserId();
        //在user_role表中删除被编辑用户的角色信息
        QueryWrapper qwUserRole=new QueryWrapper<>();
        qwUserRole.eq("user_id",uid);
        userRoleMapper.delete(qwUserRole);
        //在user_role表中添加uid用户新的角色信息
        for(Integer rid:roleIds){
            UserRole ur=new UserRole(uid,rid);
            userRoleMapper.insert(ur);
        }

        return i;
    }

    @Override
    public int delUser(Integer id) {
        //在删除用户前，删除该用户在user_role表中的信息
        QueryWrapper qw=new QueryWrapper<>();
        qw.eq("user_id",id);
        userRoleMapper.delete(qw);
        return userMapper.deleteById(id);
    }

    @Override
    public Integer batchDel(List<Integer> ids) {
        //在删除用户前，删除该用户在user_role表中的信息
        for(Integer uid:ids){
            QueryWrapper qw=new QueryWrapper<>();
            qw.eq("user_id",uid);
            userRoleMapper.delete(qw);
        }
        int i=userMapper.deleteBatchIds(ids);
        return i;
    }
}
